<template>
  <div class="container">
    <div class="top">
      <img class="head-bg" src="@/assets/images/mine/works-lib-head-bg.png" />
      <div class="info">
        <el-avatar
          :size="90"
          :src="$store.state.userInfo.avatar | imgSrc"
        ></el-avatar>
        <span class="name">{{ $store.state.userInfo.nickname }}</span>
        <el-button class="up-btn" @click="to_make_click()">{{
          t("上传作品")
        }}</el-button>
      </div>
    </div>
    <div class="content">
      <!-- <div class="search">
                <img src="@/assets/images/home/search.png" alt="">
                <input type="text" :placeholder="t('请输入搜索内容')">
                <button class="btnsearch">{{ t("搜索") }}</button>
            </div> -->
      <searchBar @search="search_click($event)"></searchBar>
      <ul class="list" v-infinite-scroll="load" style="overflow: auto">
        <li v-for="(item, idx) in list" :key="idx" class="item">
          <el-image
            class="cover"
            :src="item.image | imgSrc"
            :preview-src-list="preview_list(item)"
            fit="cover"
          ></el-image>
          <span class="name">{{ item.works_name }}</span>
        </li>
      </ul>
      <el-empty
        v-if="list.length < 1"
        description=" "
        :image="require('@/assets/images/mine/works-none.png')"
      >
        <el-button class="make-btn" type="primary" @click="to_make_click()">{{
          t("去制作")
        }}</el-button>
      </el-empty>
    </div>

    <el-dialog :visible.sync="sale_dailog" width="40%">
      <worksSale @action="sale_click($event)"></worksSale>
    </el-dialog>
  </div>
</template>
<script>
import worksSale from "@/components/worksSale.vue";
import searchBar from "@/components/searchBar.vue";
import { getUserWorksList, phoneShellIsSelect, handleWorks } from "@/utils/api";
import store from "@/store/store";
export default {
  components: {
    worksSale,
    searchBar,
  },
  data() {
    return {
      list: [],
      sale_dailog: false,
      select_goods: null,
      search_value: "",
    };
  },
  mounted() {
    this.getUserWorksList();
    phoneShellIsSelect().then((res) => {
      if (res.code == 1 && res.data) {
        this.sale_dailog = true;
        this.select_goods = res.data;
      }
    });
  },
  methods: {
    /**
     * 简化翻译方法(少写property)
     * @param {String} v 最后一个key值
     */
    t(v) {
      return this.$t(`作品库.${v}`);
    },
    load() {
      // console.log('load');
    },
    getUserWorksList() {
      getUserWorksList({
        phone_case_name: this.search_value,
      }).then((res) => {
        if (res.code == 1) {
          this.list = res.data.data;
        }
      });
    },
    to_make_click() {
      this.$router.push("worksMake");
    },
    preview_list(item) {
      return [process.env.VUE_APP_IMG_ROOT + item.image];
    },
    sale_click(e) {
      this.sale_dailog = false;
      handleWorks({
        user_works_id: this.select_goods.id,
        style: Number(e),
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
        }
      });
    },
    search_click(e) {
      console.log(e);
      this.search_value = e;
      this.getUserWorksList();
    },
  },
};
</script>
<style lang="less" scoped>
::v-deep .el-image-viewer__img {
  width: 672px !important;
  height: 870px !important;
}

.container {
  background: #fff;

  .top {
    width: 100%;
    position: relative;

    .head-bg {
      width: 100%;
      object-fit: cover;
    }

    .info {
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 52%;
      position: absolute;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-direction: row;

      .name {
        flex: 1;
        color: #222;
        font-size: 22px;
        font-weight: 500;
        margin-left: 20px;
      }

      .up-btn {
        border-radius: 10px;
        background: #ff5757;
        color: #fff;
        font-size: 20px;
      }
    }
  }

  .content {
    margin-top: 33px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;

    .search {
      width: 52%;
      height: 60px;
      background: #f9f9f9;
      border: 1px solid #f1f1f1;
      box-shadow: 0px 0px 10px 0px rgba(227, 227, 227, 0.41);
      border-radius: 30px;
      overflow: hidden;
      display: flex;
      align-items: center;

      img {
        width: 25px;
        height: 25px;
        margin-left: 10px;
      }

      input {
        width: 82%;
        height: 100%;
        outline: none;
        border: 0;
        background-color: transparent;
        text-indent: 2em;
        font-size: 20px;
        font-weight: 400;
        color: #999999;
      }

      .btnsearch {
        width: 15%;
        height: 54px;
        background: #ff5757;
        border-radius: 30px;
        font-size: 20px;
        font-weight: 400;
        color: #ffffff;
        text-align: center;
        line-height: 54px;
        border: 0;
        outline: 0;
      }
    }

    .list {
      margin-top: 80px;
      display: grid;
      grid-template-columns: repeat(5, 300px);
      // grid-auto-rows: 370px;
      // grid-auto-columns: 300px;
      gap: 20px;
      grid-auto-flow: row;

      .item {
        width: 298px;
        height: 410px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        flex-direction: column;
        margin-right: 52px;

        .cover {
          width: 298px;
          height: 370px;
          object-fit: contain;
          border-radius: 10px;
        }

        .name {
          margin-top: 20px;
          color: #222;
          font-size: 20px;
          font-weight: 400;
        }
      }
    }

    .make-btn {
      background: #000;
      font-size: 20px;
      border-radius: 10px;
      color: #fff;
      font-weight: 400;
    }
  }
}
</style>
<style>
.el-image-viewer__img {
  width: 672px !important;
  height: 870px !important;
}
</style>
